package com.kanshu.ksgb.fastread.doudou.module.bookcity.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

import com.dl7.recycler.adapter.BaseQuickAdapter;
import com.dl7.recycler.adapter.BaseViewHolder;
import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.app.constants.ARouterConfig;
import com.kanshu.ksgb.fastread.doudou.app.constants.Constants;
import com.kanshu.ksgb.fastread.doudou.common.imageloader.GlideImageLoader;
import com.kanshu.ksgb.fastread.doudou.common.util.ARouterUtils;
import com.kanshu.ksgb.fastread.doudou.common.business.commonbean.BookInfo;
import com.kanshu.ksgb.fastread.doudou.common.business.utils.BookUtils;
import com.qq.e.ads.nativ.NativeExpressADView;

import java.util.List;

/**
 * 每周强推适配器
 * Created by wcy on 18/5/17.
 */
public class MaleOrFemaleWeekAdapter extends BaseQuickAdapter<BookInfo> {
    private int mTabType;
    public MaleOrFemaleWeekAdapter(Context context, List<BookInfo> data, int tabType) {
        super(context, data);
        mTabType = tabType;
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.item_male_or_female_week_layout;
    }

    @Override
    protected void convert(BaseViewHolder holder, BookInfo item, int position) {
        if(item.adview != null) {
            ViewParent parent = item.adview.getParent();
            if (parent != null) {
                ViewGroup viewGroup = (ViewGroup) parent;
                viewGroup.removeView(item.adview);
            }
            ViewGroup container = holder.getView(R.id.root);
            container.removeAllViews();
            container.addView(item.adview);
            int height = container.getHeight();
            int width = container.getWidth();
            Log.i("test", "height " + height + " width " + width);
            return;
        }
        GlideImageLoader.load(item.cover_url, holder.getView(R.id.cover));
        holder.setText(R.id.book_title, item.book_title).setText(R.id.desc, item.book_intro)
                .setText(R.id.author, item.author_name).setText(R.id.book_label, BookUtils.getBookLabel(item))
                .setText(R.id.book_status, BookUtils.getBookStatus(item));
        holder.setVisible(R.id.bottom_container, true);
        holder.setVisible(R.id.desc, true);
        int label, status;
        if (mTabType == Constants.BookCityTabType.TYPE_MALE) {
            label = R.mipmap.ic_male_label;
            status = R.mipmap.ic_male_status;
        } else {
            label = R.mipmap.ic_female_label;
            status = R.mipmap.ic_female_status;
        }
        holder.setBackgroundRes(R.id.book_label, label);
        holder.setBackgroundRes(R.id.book_status, status);
        holder.getConvertView().setOnClickListener(v -> {
            ARouterUtils.toActivity(ARouterConfig.BOOK_DETAIL, "book_id", item.book_id);
        });

    }

    @Override
    public int getItemViewType(int position) {
        int type = super.getItemViewType(position);
        if(type == 0 ){
            BookInfo info = getData().get(position - getHeaderViewsCount());
            if(info.adview !=null){
                type = EMPTY_CONTAINER;
            }
        }
        return type;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mParentView == null) {
            mParentView = parent;
        }
        BaseViewHolder baseViewHolder;
        switch (viewType) {
            case LOADING_VIEW:
                baseViewHolder = new BaseViewHolder(mLoadingView);
                break;
            case HEADER_VIEW:
                baseViewHolder = new BaseViewHolder(mHeaderView);
                break;
            case EMPTY_VIEW:
                baseViewHolder = new BaseViewHolder(mEmptyView);
                break;
            case FOOTER_VIEW:
                baseViewHolder = new BaseViewHolder(mFooterView);
                break;
            case EMPTY_CONTAINER:
                View view = mLayoutInflater.inflate(R.layout.ad_empty_container,parent,false);
                baseViewHolder = new BaseViewHolder(view);
                break;
            default:
                baseViewHolder = onCreateDefViewHolder(parent, viewType);
                // 设置用于单项刷新的tag标识
                baseViewHolder.itemView.setTag(R.id.view_holder_tag, baseViewHolder);
                _initItemClickListener(baseViewHolder);
                break;
        }
        return baseViewHolder;
    }

    public void removeAdItem(int position, NativeExpressADView adView){
        int adPosition = -1;
        int index = 0;
        for (BookInfo book: mData ) {
            if(adView == book.adview) {
                adPosition = index;
                break;
            }
            index++;
        }
        if(adPosition>=0){
            mData.remove(adPosition);
            notifyDataSetChanged();
        }
    }
}
